import { Form, Input, Modal, Button, message, Radio } from "antd";
import React, { Component } from "react";
const layout = {
  labelCol: {
    span: 8,
  },
  wrapperCol: {
    span: 14,
  },
};
const tailLayout = {
  wrapperCol: {
    offset: 8,
    span: 14,
  },
};
const formRef = React.createRef();
export class PriceForm extends Component {
  handleClose = (e) => {
    console.log(e);
    this.props.close();
  };
  state = {
    show: this.props.show,
    submitLoading: false,
    formData: this.props.formData,
  };
  static getDerivedStateFromProps(nextProps) {
    if (formRef.current) {
      formRef.current.setFieldsValue(nextProps.formData);
    }
    return {
      show: nextProps.show,
      formData: nextProps.formData,
    };
  }
  onFinish = async (values) => {
    // console.log(this.state.formData, values);
    // return;
    this.setState({ submitLoading: true });
    let res;
    if (this.state.formData.accountCate !== undefined) {
      values.id = this.state.formData.id;
      values.machineSizeId = this.state.formData.machineSizeId;

      res = await this.http("MachinePrice", values, "POST");
    } else {
      values.machineSizeId = this.state.formData.id;
      res = await this.http("MachinePrice", values, "POST");
    }
    console.log(res);
    this.props.close();
    this.props.refresh();
    this.setState({
      submitLoading: false,
    });
    message.success("操作成功！");
  };
  render() {
    return (
      <Modal
        title="修改配置"
        footer={[null, null]}
        onCancel={this.handleClose}
        visible={this.state.show}
      >
        <Form
          ref={formRef}
          initialValues={this.state.formData}
          {...layout}
          name="basic"
          onFinish={this.onFinish}
          onFinishFailed={this.onFinishFailed}
        >
          <Form.Item
            label="价格"
            name="price"
            rules={[
              {
                required: true,
                message: "矿机价格不可为空！",
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="付款方式"
            name="accountCate"
            rules={[
              {
                required: true,
                message: "矿机种类不可为空！",
              },
            ]}
          >
            <Radio.Group
            // onChange={onChange} value={value}
            // defaultValue={this.state.formData.accountCate || 1}
            >
              <Radio value={1}>USDT</Radio>
              <Radio value={2}>CSKT</Radio>
              <Radio value={3}>FIL</Radio>
              <Radio value={5}>CHIA</Radio>
              <Radio value={6}>BZZ</Radio>
              {/* <Radio value={4}>其他</Radio> */}
            </Radio.Group>
          </Form.Item>

          <Form.Item {...tailLayout}>
            <Button
              loading={this.state.submitLoading}
              type="primary"
              htmlType="submit"
            >
              提交
            </Button>
          </Form.Item>
        </Form>
      </Modal>
    );
  }
}
